#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/kernel.h>   /* printk() */
#include <linux/miscdevice.h> /* misc... */
#include <linux/fs.h>       /* everything... */
#include <linux/types.h>    /* size_t */

#include <linux/uaccess.h>

int misc_open(struct inode *inode,struct file *file)
{
    printk( "open my_misc_dev\n");
    return 0;
}

int misc_release(struct inode *inode,struct file *file)
{
    printk( "release my_misc_dev\n");
    return 0;
}

ssize_t misc_read(struct file *file,char __user *ubuf,size_t size,loff_t *loff_t)
{
    char kbuf[64] = "xieshangle";

    if(copy_to_user(ubuf,kbuf,strlen(kbuf))!= 0)
    {
        printk( "copy_to_user error\n");
        return -1;
    }
    printk( "misc_read\n");
    return 0;
}

ssize_t misc_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loff_t)
{
    printk( "misc_write\n");
    return 0;
}


struct file_operations misc_fops={
  .owner   =THIS_MODULE,
  .open    =misc_open,
  .release =misc_release,
  .read    =misc_read,
  .write   = misc_write,
};

struct miscdevice misc_dev ={
	.minor =MISC_DYNAMIC_MINOR,
	.name  ="my_misc",
	.fops  =&misc_fops,

};

static int misc_init(void)
{
    int ret;
    ret =misc_register(&misc_dev);
    if(ret < 0){
        printk( "misc register is failed\n");
        return -1;
    }
    printk( "misc register is succeed\n");
    return 0;

}
static void misc_exit(void)
{
    misc_deregister(&misc_dev);
    printk(KERN_ALERT "Goodbye,misc\n");
}

/* register the init and exit routine of the module */
module_init( misc_init );
module_exit( misc_exit );

MODULE_LICENSE("GPL");
